package com.now.printer.base;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.now.printer.R;
import com.now.printer.adapter.menu.DrawerAdapter;
import com.now.printer.adapter.menu.DrawerItem;
import com.now.printer.adapter.menu.SimpleItem;
import com.now.printer.adapter.menu.SpaceItem;
import com.now.printer.ui.activity.AboutUsActivity;
import com.now.printer.ui.activity.FileBrowerManagerActivity;
import com.now.printer.ui.activity.HelpActivity;
import com.now.printer.ui.activity.PrintHistoryRecordActivity;
import com.now.printer.utils.ResUtils;
import com.now.printer.utils.Utils;
import com.umeng.analytics.MobclickAgent;
import com.xuexiang.xutil.resource.ThemeUtils;
import com.xuexiang.xutil.tip.ToastUtils;
import com.yarolegovich.slidingrootnav.SlideGravity;
import com.yarolegovich.slidingrootnav.SlidingRootNav;
import com.yarolegovich.slidingrootnav.SlidingRootNavBuilder;
import com.yarolegovich.slidingrootnav.callback.DragStateListener;

import java.util.Arrays;

import butterknife.ButterKnife;

/**
 * Created by xudong on 2021/5/13.
 */

public abstract class BaseActivity2 extends AppCompatActivity implements DrawerAdapter.OnItemSelectedListener {

    protected static final int POS_HOME = 0;
    protected static final int POS_MINE = 1;
    protected static final int POS_MORE = 2;
    protected static final int POS_ABOUT = 3;
    protected static final int POS_WENTI = 4;
    protected static final int POS_LOGOUT = 5;

    private SlidingRootNav mSlidingRootNav;

    protected Bundle savedInstanceState;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        initAppTheme();
        initStatusBarStyle();
        super.onCreate(savedInstanceState);
        this.savedInstanceState = savedInstanceState;
//        /**判断版本，如果[4.4~5.0)就设置状态栏和导航栏为透明**/
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT &&
//                Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
//            //设置虚拟状态栏为透明
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            //设置虚拟导航栏为透明
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//        }
        View view = LayoutInflater.from(this).inflate(getLayoutId(),null);
        setContentView(view);
        //注册注解
        ButterKnife.bind(this);

        if (mMenuTitles == null)
        mMenuTitles = ResUtils.getStringArray(R.array.menu_titles);
        if (mMenuIcons == null)
            mMenuIcons = ResUtils.getDrawableArray(this, R.array.menu_icons);

        doBusiness(this);

    }

    /**
     * [绑定布局]
     *
     * @return
     */
    public abstract int getLayoutId();

    /**
     * [业务操作]
     *
     * @param mContext
     */
    public abstract void doBusiness(Context mContext);


    /**
     * 初始化应用的主题
     */
    protected void initAppTheme() {
        Utils.initTheme(this);
    }

    /**
     * 初始化状态栏的样式
     */
    protected void initStatusBarStyle() {

    }

    /**
     * 根据传入的Class
     *
     * @param pClass
     */
    public void openNewActivity(Class<?> pClass) {
        Intent _Intent = new Intent();
        _Intent.setClass(this, pClass);
        startActivity(_Intent);
    }

    private String[] mMenuTitles;
    private Drawable[] mMenuIcons;
    private DrawerAdapter mAdapter;

    public void initSlidingMenu(Bundle savedInstanceState) {
        if (mSlidingRootNav == null) {
            mSlidingRootNav = new SlidingRootNavBuilder(this)
                    .withGravity(ResUtils.isRtl() ? SlideGravity.RIGHT : SlideGravity.LEFT)
                    .withMenuOpened(false)
                    .withContentClickableWhenMenuOpened(false)
                    .withSavedState(savedInstanceState)
                    .withMenuLayout(R.layout.menu_left_drawer)
                    .inject();

            LinearLayout mLLMenu = mSlidingRootNav.getLayout().findViewById(R.id.ll_menu);

            mAdapter = new DrawerAdapter(Arrays.asList(
                    createItemFor(POS_HOME).setChecked(true),
                    createItemFor(POS_MINE),
                    createItemFor(POS_MORE),
                    createItemFor(POS_ABOUT)
//                    createItemFor(POS_WENTI),
//                    new SpaceItem(48),
//                    createItemFor(POS_LOGOUT)
            ));
            mAdapter.setListener(this);

            RecyclerView list = findViewById(R.id.list);
            list.setNestedScrollingEnabled(false);
            list.setLayoutManager(new LinearLayoutManager(this));
            list.setAdapter(mAdapter);

//            mAdapter.setSelected(POS_HOME);
            mSlidingRootNav.setMenuLocked(false);
            mSlidingRootNav.getLayout().addDragStateListener(new DragStateListener() {
                @Override
                public void onDragStart() {
                }
                @Override
                public void onDragEnd(boolean isMenuOpened) {
                }
            });
        }
    }

    private DrawerItem createItemFor(int position) {
        return new SimpleItem(mMenuIcons[position], mMenuTitles[position])
                .withIconTint(getResources().getColor(R.color.white))
                .withTextTint(getResources().getColor(R.color.white))
                //ThemeUtils.resolveColor(this, R.attr.colorAccent)
                .withSelectedIconTint(getResources().getColor(R.color.white))
                .withSelectedTextTint(getResources().getColor(R.color.white));

    }

    public void openMenu() {
        if (mSlidingRootNav != null) {
            mSlidingRootNav.openMenu();
        }
    }

    public void closeMenu() {
        if (mSlidingRootNav != null) {
            mSlidingRootNav.closeMenu();
        }
    }

    public boolean isMenuOpen() {
        if (mSlidingRootNav != null) {
            return mSlidingRootNav.isMenuOpened();
        }
        return false;
    }

    @Override
    public void onItemSelected(int position) {
        switch (position) {
            case POS_HOME:
                Intent intent1 = new Intent(this, FileBrowerManagerActivity.class);
                intent1.putExtra("type", "1");//0是所有，1是文件浏览，2是图片
                startActivity(intent1);
                break;
            case POS_MINE:
                Intent intent2 = new Intent(this, PrintHistoryRecordActivity.class);
                startActivity(intent2);
                break;
            case POS_MORE:
//                mTabLayout.selectTab(component);
                Intent intent3 = new Intent(this, HelpActivity.class);
                startActivity(intent3);
                break;
            case POS_ABOUT:
                startActivity(new Intent(this, AboutUsActivity.class));
                break;
//                        case POS_MORE:
//                if (mTabLayout != null) {
//                    TabLayout.Tab tab = mTabLayout.getTabAt(position);
//                    if (tab != null) {
//                        tab.select();
//                    }
//                }
//                mSlidingRootNav.closeMenu();
//                break;

//            case POS_WENTI:
//                mTabLayout.selectTab(component);
//                break;

            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == XXPermissions.REQUEST_CODE) {
            if (XXPermissions.isGranted(this, Permission.RECORD_AUDIO) &&
                    XXPermissions.isGranted(this, Permission.Group.CALENDAR)) {
                ToastUtils.toast("用户已经在权限设置页授予了权限");
            } else {
                ToastUtils.toast("用户没有在权限设置页授予权限");
            }
        }
    }
    /**
     *  确保在所有的Activity中都调用 MobclickAgent.onResume和onPause方法
     *  （如果是在AUTO页面采集模式下，则需要注意，所有Activity中都不能调用MobclickAgent.onResume和onPause方法，详见后续页面采集章节），
     *  这两个调用不会阻塞应用程序的主线程，也不会影响应用程序的性能。 页面采集模式的设置不仅影响采集页面，也影响了新增启动活跃数据，
     *  因为其作用是对App整个生命周期的监控。您的用户可能从任何activity进入您的App，需要对所有activity进行完整的监控。
     *  如果您选择的模式是AUTO，初始化函数是默认对所有activity自动埋点，因此不需要调用MobclickAgent.onResume和onPause方法。
     *  如果您的Activity之间有继承或者控制关系，请不要同时在父和子Activity中重复添加nPause和nResume方法，
     *  否则会造成重复统计，导致启动次数异常增高。(例如：使用TabHost、TabActivity、ActivityGroup时)。
     *  当应用在后台运行超过30秒（默认）再回到前台，将被认为是两次独立的Session(启动)，例如：用户回到home，
     *  或进入其他程序，经过一段时间后再返回之前的应用。即被认为是两个独立的Session。
     */

//    @Override
//    public void onResume(){
//        super.onResume();
//        MobclickAgent.onResume(this);
//    }
//
//    @Override
//    public void onPause(){
//        super.onPause();
//        MobclickAgent.onPause(this);
//    }

      protected View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.imgHelp:
                    Intent intent3 = new Intent(BaseActivity2.this, HelpActivity.class);
                    startActivity(intent3);
                    break;
            }
        }
    };
}
